<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/5
 * Time: 10:58
 */

defined('BASEPATH') OR exit('No direct script access allowed');

class Delivery extends Admin_Controller
{
    public function index()
    {
        $this->load->model('order_model');
        $this->load->model('siteweb_model');

        $this->load->library('pagination');
        $this->config->load('data_field');

        $order = $this->config->item('order');
        $assoc = $this->uri->uri_to_assoc();

        $_param_option = array(
            'goods_name' => array(
                'col' => 'col-lg-3',
                'type' => 'text-c',
                'placeholder' => '输入订单号、email、姓名',
                'item' => array(
                    'name' => 'web_id',
                    'placeholder' => '选择站点',
                    'option' => $this->siteweb_model->siteweb_option()
                )
            ),
            'pay_time' => array(
                'col' => 'col-lg-4',
                'type' => 'dpd_time',
                'placeholder' => '付款时间'
            ),
            'order_amount' => array(
                'col' => 'col-lg-2',
                'type' => 'dpd',
                'placeholder' => '总金额'
            ),
            'shipping_name' => array(
                'col' => 'col-lg-3',
                'type' => 'option',
                'placeholder' => '选择配送方式',
                'option' => $order['payment_name']
            )
        );

        $_param = array(
            'shipping_name' => trim($this->input->get('shipping_name')),
            'order_amount_start' => trim($this->input->get('order_amount_start')),
            'order_amount_end' => trim($this->input->get('order_amount_end')),
            'pay_time_start' => trim($this->input->get('pay_time_start')),
            'pay_time_end' => trim($this->input->get('pay_time_end')),
            'web_id' => intval($this->input->get('web_id')),
            'goods_name' => htmlspecialchars(trim($this->input->get('goods_name'))),
            'deliver' => 1,
            'page' => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0
        );

        $config['base_url'] = base_url('delivery/index/') . 'page/';
        $config['total_rows'] = $this->order_model->order_num($_param);
        $config['num_links'] = 6;
        $config['per_page'] = $this->order_model->limit_rows;
        $config['use_page_numbers']=true;

        $current_page = intval($assoc['page']);

        $this->pagination->initialize($config);

        $_action_link = array(
            array('href' => base_url('delivery/export_order') . '?' . $_SERVER["QUERY_STRING"], 'name' => '导出订单')
        );

        $data = array(
            'order_list' => $this->order_model->order_list($_param),
            'pages' => $this->pagination->create_links(),
            'param' => $_param,
            'param_option' => $_param_option,
            'action_link' => $_action_link
        );

        $this->load->view('delivery_goods_list', $data);
    }

    
    public function order_ex()
    {
        $this->load->model('order_model');
    
        $_param = array(
            'order_amount_start' => trim($this->input->get('order_amount_start')),
            'order_amount_end' => trim($this->input->get('order_amount_end')),
            'pay_time_start' => trim($this->input->get('pay_time_start')),
            'pay_time_end' => trim($this->input->get('pay_time_end')),
            'web_id' => intval($this->input->get('web_id')),
            'order_sn' => htmlspecialchars(trim($this->input->get('goods_name'))),
            'export_order' => 1
        );
    
        $order_list = $this->order_model->order_list($_param, 1);
    
        $data = "物流网络,参考号,目的地,收件省州,收件城市,收件人,收件邮编,收件地址,收件电话,收件邮箱,物品描述,重量,英文描述,物品数量,物品单价,包裹数量,包裹类型\n";
    
        foreach ($order_list as $row) {
            $row['total_weight'] = $row['total_weight'] / 1000;
            $row['zip'] = preg_replace('/[^0-9a-zA-z\-]/','',$row['zip']);
            $row['address'] = strtr($row['address'], ",", " ");
            $data .= "SH国际E邮宝,#{$row['order_sn']},{$row['country']},{$row['province']},{$row['city']},{$row['user_name']},'{$row['zip']},{$row['address']},{$row['phone']},{$row['email']},,{$row['total_weight']},{$row['goods_name']} - {$row['order_sn']},{$row['goods_number']},20,1,包裹\n";
        }
    
        $file_name = date('YmdHis',time()) . '.csv';
        header('Content-Type: application/download');
        header("Content-type:text/csv;");
        header("Content-Disposition:attachment;filename=" . $file_name);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        $csvContent = mb_convert_encoding($data,'gb2312','utf-8');
        echo $csvContent;
        exit();
    }
    
    public function export_order()
    {
        $this->load->model('order_model');

        $_param = array(

            'order_amount_start' => trim($this->input->get('order_amount_start')),
            'order_amount_end' => trim($this->input->get('order_amount_end')),
            'pay_time_start' => trim($this->input->get('pay_time_start')),
            'pay_time_end' => trim($this->input->get('pay_time_end')),
            'web_id' => intval($this->input->get('web_id')),
            'goods_name' => htmlspecialchars(trim($this->input->get('goods_name'))),
            'deliver' => 1,
            'export_order' => 1
        );

        $order_list = $this->order_model->order_list($_param, 1);

        $data = "物流网络,参考号,目的地,收件省州,收件城市,收件人,收件邮编,收件地址,收件电话,收件邮箱,物品描述,重量,英文描述,物品数量,物品单价,包裹数量,包裹类型\n";

        foreach ($order_list as $row) {
            $row['total_weight'] = $row['total_weight'] / 1000;
            $row['zip'] = preg_replace('/[^0-9a-zA-z\-]/','',$row['zip']);
            $row['address'] = strtr($row['address'], ",", " ");
            $data .= "SH国际E邮宝,#{$row['order_sn']},{$row['country']},{$row['province']},{$row['city']},{$row['user_name']},'{$row['zip']},{$row['address']},{$row['phone']},{$row['email']},,{$row['total_weight']} - {$row['orer_sn']},{$row['goods_name']},{$row['goods_number']},20,1,包裹\n";
        }

        $file_name = date('YmdHis',time()) . '.csv';
        header('Content-Type: application/download');
        header("Content-type:text/csv;");
        header("Content-Disposition:attachment;filename=" . $file_name);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        $csvContent = mb_convert_encoding($data,'gb2312','utf-8');
        echo $csvContent;
        exit();
    }

    public function in_shipping_no()
    {
        if ($_FILES) {

        }

        $this->load->view('delivery_in_shipping_no');
    }
}